using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class SettingDialog : DialogBase
{
    [SerializeField]private Slider musicSlider;
    [SerializeField]private Slider sfxSlider;
    private AudioSource musicSource;
    private AudioSource sfxSource;

    private float musicSetData;
    private float sfxSetData;
    // Start is called before the first frame update
    void Start()
    {
        musicSource=GameObject.FindGameObjectWithTag("MainCamera").GetComponent<AudioSource>();
        if(GameObject.FindGameObjectWithTag("AudioManager")){
            sfxSource=GameObject.FindGameObjectWithTag("AudioManager").GetComponent<AudioSource>();
        }

        musicSlider.onValueChanged.AddListener(MusicChanged);
        sfxSlider.onValueChanged.AddListener(SfxChanged);

        SetFromData();

    }

    private void SetFromData()
    {
        DataManager.instance.LoadMusicSetting();
        DataManager.instance.LoadSfxSetting();
        musicSetData=DataManager.instance.musicSetting;
        sfxSetData=DataManager.instance.sfxSetting;

        musicSlider.value=musicSetData;
        if(sfxSource!=null){
            sfxSource.volume=musicSetData;
        }

        sfxSlider.value=sfxSetData;
        if(sfxSource!=null){
            sfxSource.volume=sfxSetData;
        }
    }

    private void MusicChanged(float value)
    {
        if(musicSource!=null){
           musicSource.volume=value; 
        }
        DataManager.instance.musicSetting=value;
        DataManager.instance.SaveMusicSetting();
        
    }

    private void SfxChanged(float value)
    {
        if(sfxSource!=null){
           sfxSource.volume=value; 
        }
        DataManager.instance.sfxSetting=value;
        DataManager.instance.SaveSfxSetting();
        
    }
    
}
